1619E - MEX and Increments - CodeForces Solution


constructive algorithms data structures dp greedy implementation math sortings *1700

Please click on ads to support us..

Python Code:

import bisect
for _ in range(int(input())):
    n=int(input())
    a=sorted(map(int,input().split()))
    m=set()
    e=[]
    w={}
    for i in a:
        if i not in m:
            m.add(i)
        else:
            e+=[i]
        w[i]=w.get(i,0)+1
    s=0
    r=[]
    t=0
    for i in range(n+1):
        if t:
            r+=[-1]
        else:
            if i not in m:
                o=bisect.bisect_right(e,i)
                if o==0:
                    t=1
                    r+=[s]
                else:
                    r+=[s]
                    s+=i-e[o-1]
                    e.pop(o-1)
            else:
                r+=[w[i]+s]
    print(*r)
                
            
            

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second 
#define N 300005
const int mod = 998244353;
template<typename T>void print(vector<T>&a,int c=0) {
    for(auto&it :a)cout << it+c<< " ";
    cout << endl;
}
void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

ll prime[N];
void sieve() {
    memset(prime,0,sizeof(prime));
    for(ll i=2;i<N;i++) {
        if(!prime[i]) {
            prime[i] = i;
            for(ll j = i*i;j<N;j+=i) {
                prime[j] = i;
            }
        }
    }
}
bool isPrime(int a) {
    if(a <= 1) return false;
    if(a <= 3) return true ;
    if(a%2 == 0 || a%3 == 0) return false;
    
    for(int i=5;i*i<=a;i+=6) {
        if(a%i == 0 || a%(i+2) == 0) return false;
    }
    return true ;
}

ll pow(ll a, ll b, ll m = 998244353) {
    if(b == 0) return 1;
    ll ans = pow(a, b / 2, m);
    
    if(b&1) return (ans * ans * a) % m;
    else return (ans * ans) % m;
}

int main(){
    fast();
    
    /* Code starts here -------------------------------------------> */
    int t;cin>>t;
    while(t--) {
        int n;cin>>n;
        vector<int>a(n);
        vector<ll>cnt(n + 1, 0), res(n + 1, -1);
        for(auto&it : a) {
            cin>>it ;
            cnt[it]++;
        }
        ll ans = 0;
        int op = 0;
        vector<pair<int ,int>>tmp;
        for(int i=0;i<=n;i++) {
            if(op != i) break;
            res[i] = 1LL * (ans + cnt[i]);
            if(cnt[i] == 0) {
                if(tmp.size() != 0) {
                    ans += 1LL * (i - tmp.back().ff);
                    ++op;
                    if(--tmp.back().ss == 0) {
                        tmp.pop_back();
                    }
                }
            }else {
                ++op;
                if(cnt[i] > 1) tmp.push_back({i, cnt[i] - 1});
            }
        }
        print(res);
    }
    
    
    
    
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment